# 导包
from flask import Flask, app

# 导入基本模块

from flask import request, jsonify
import jwt

from views.comany import bp_company
from views.user import bp_user



# 跨域模块
from flask_cors import CORS

import logging
from logging import FileHandler

import datetime


from utils.jwtc import myjwt

from utils.redis import rs


# 创建app
def create_app():

    # 建立 flask实例
    app = Flask(__name__)

    with app.app_context():
        pass
    return app

app = create_app()
app.register_blueprint(bp_company)
app.register_blueprint(bp_user)

# 利用跨域模块进行封装
CORS(app,cors_allowed_origins="*")

# 解决中文乱码
app.config['JSON_AS_ASCII'] = False


# @app.before_request

# def before_request():

#     # 定义白名单
#     white = ['/login']

#     if request.path not in white:

#         # 401过期 403标识没获取token
#         try:

#             token = request.headers['Token']
        
#         except:

#             return jsonify({"code":403,"mes":"无权访问"})

#         # 调用decode解析token
#         if myjwt.jwt_check(token) == False:
            
#             return jsonify({"code":402,"mes":"token不合法"})

#         # 权限验证
#         payload = myjwt.jwt_en(token)
#         uid = payload['data']["userid"]
#         key = 'interface'+str(uid)

#         # 根据用户id获取接口权限
#         list = rs.find_set(key)

#         if request.path not in list:

#             return jsonify({"code":40301,"mes":"无权访问"})
            

if __name__ == "__main__":

    filename = datetime.datetime.strftime(datetime.datetime.now(),'%Y%m')

    Handler = logging.FileHandler("logs/"+filename+'.txt')

    app.logger.addHandler(Handler)

    app.run(debug=True)


